﻿using System;
using System.Collections.Generic;
using System.IO;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKArch.BalAndStepModule.CreateFootstep
{
	public class MassingFamilyDesigner
	{
		public MassingFamilyDesigner(ExternalCommandData cmdData, ElementId materialId)
		{
			this.m_Revit = cmdData;
			this.m_RevitApp = this.m_Revit.Application.Application;
			this.m_MaterialId = materialId;
		}

		public Result DesignExtrusionFootstep(List<List<LoopEdge>> hierarchicalLoopEdge, double stepHeight, ref string familyFilePath, ref string familyName, ref string symbolName, ref string message)
		{
			Result result = this.OpenAndActiveFamilyTemplateDocument(ref message);
			if (result != null)
			{
				return result;
			}
			Transaction transaction = new Transaction(this.m_FamilyDocument);
			transaction.Start("create family");
			this.m_FamilyCreator = new ExtrusionFootstepFamilyCreator(this.m_Revit, this.m_MaterialId, this.m_FamilyDocument);
			hierarchicalLoopEdge.Reverse();
			result = this.CreateExtrusionFootstep(hierarchicalLoopEdge, stepHeight, ref symbolName, ref message);
			if (result != null)
			{
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				this.DocumentClose();
				return result;
			}
			transaction.Commit();
			result = this.SaveFamilyFile(ref familyFilePath, ref familyName);
			if (result != null)
			{
				this.DocumentClose();
				return result;
			}
			return result;
		}

		private Result CreateExtrusionFootstep(List<List<LoopEdge>> hierarchicalLoopEdge, double stepHeight, ref string symbolName, ref string message)
		{
			Result result = this.m_FamilyCreator.AddReferencePlanes(ref message);
			if (result != null)
			{
				return result;
			}
			result = this.m_FamilyCreator.CreateExtrusion(hierarchicalLoopEdge, stepHeight, ref message);
			if (result != null)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddAlignments(ref message);
			if (result != null)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddParameters(ref message);
			if (result != null)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddDimensions(ref message);
			if (result != null)
			{
				return result;
			}
			result = this.m_FamilyCreator.AddSymbols(ref symbolName, ref message);
			if (result != null)
			{
				return result;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Result OpenAndActiveFamilyTemplateDocument(ref string message)
		{
			string text = AssistFunc.GetAppRootPath();
			string versionNumber = this.m_Revit.Application.Application.VersionNumber;
			text = text + "\\Templates\\" + versionNumber + "\\公制常规模型.rft";
			if (!File.Exists(text))
			{
				throw new Exception("Unsupported version");
			}
			this.m_FamilyDocument = this.m_RevitApp.NewFamilyDocument(text);
			if (this.m_FamilyDocument == null)
			{
				message = "族模板目录缺少‘公制常规模型.rft’文件，无法创建族文档.";
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Result SaveFamilyFile(ref string familyFilePath, ref string familyName)
		{
			int num = this.CalculateFamilyNumber();
			string tempPath = Path.GetTempPath();
			familyName = "HYFootstep" + num.ToString();
			familyFilePath = tempPath + familyName + ".rfa";
			if (File.Exists(familyFilePath))
			{
				File.Delete(familyFilePath);
			}
			RevitVersionFuncs.SaveAs(this.m_FamilyDocument, familyFilePath);
			this.m_FamilyDocument.Close(true);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private int CalculateFamilyNumber()
		{
			string text = "HYFootstep";
			int length = text.Length;
			List<int> list = new List<int>();
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(Family)).ToElements())
			{
				Family family = element as Family;
				if (family != null)
				{
					string name = family.Name;
					int num = name.IndexOf(text);
					if (num != -1)
					{
						string value = name.Substring(num + length, name.Length - num - length);
						int item = 0;
						try
						{
							item = Convert.ToInt32(value);
						}
						catch (Exception)
						{
							continue;
						}
						list.Add(item);
					}
				}
			}
			if (list.Count == 0)
			{
				return 1;
			}
			list.Sort();
			return list[list.Count - 1] + 1;
		}

		private void DocumentClose()
		{
			this.m_FamilyDocument.Close(false);
		}

		private ExternalCommandData m_Revit;

		private Autodesk.Revit.DB.Document m_FamilyDocument;

		private MassingFamilyCreator m_FamilyCreator;

		private ElementId m_MaterialId;

		private Autodesk.Revit.ApplicationServices.Application m_RevitApp;
	}
}
